package com.atguigu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.QueryAnnotation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-06-09
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin//解决跨域问题
public class EduTeacherController {
    //访问地址：http://localhost:8001/eduservice/teacher/findAll
    @Autowired
    private EduTeacherService teacherService;

    //1.查詢所有講師
    @ApiOperation(value = "所有講師列表")
    @GetMapping("findAll")
    public Result findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items",list);
    }
    //逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("removeTeacher/{id}")
    public Result removeTeacher(@ApiParam(name="id",value = "讲师ID",required = true)@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "分页查询讲师")
    //分页查询讲师的方法
    @GetMapping("pageTeacher/{current}/{limit}")
    public Result pageTeacher(@ PathVariable long current,@PathVariable long limit){
        Page<EduTeacher> page = new Page<>(current,limit);

        teacherService.page(page,null);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return Result.ok().data("total",total).data("items",records);
    }

    //条件查询带分页的方法
//    @PostMapping()
    @RequestMapping(value="pageTeacherCondition/{current}/{limit}",method=RequestMethod.POST)
    public Result pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                        @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(name !=null ){
            wrapper.like("name",name);
        }
        if (level != null){
            wrapper.eq("level",level);//等于
        }
        if(begin != null && end != null){
            wrapper.between("gmt_create",begin,end);
        }
        teacherService.page(pageTeacher,wrapper);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return Result.ok().data("total",total).data("items",records);



    }

    //添加讲师操作
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
       return save?Result.ok():Result.error();
    }

    //根据讲师id查询
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable String id){

        EduTeacher eduteacher = teacherService.getById(id);
        return Result.ok().data("teacher",eduteacher);
    }
    //修改讲师的信息
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if (b){
            return Result.ok();
        }
        return Result.error();
    }
















}

